<?php
	$rootdir = '../';

	include_once('session.php');
	include_once('codespromo.php');
	include_once('panier.php');
	include_once('pays.php');
	
	if(isset($_SESSION['user']['currentorder']) == false)
		echo 'Code invalide';
		
	if(isset($_GET['code']) == false)
	{
		$_SESSION['user']['currentorder']['codepromo_code'] = '';
		$_SESSION['user']['currentorder']['codepromo_type'] = '';
		$_SESSION['user']['currentorder']['codepromo_param'] = '';
		$_SESSION['user']['currentorder']['frais_port_apresreduction'] = $_SESSION['user']['currentorder']['frais_port_avantreduction'];
		$_SESSION['user']['currentorder']['montantpanier_apresreduction'] = $_SESSION['user']['currentorder']['montantpanier_avantreduction'];
		echo 'Code invalide';
	}
	else
	{
		$code = GetCodePromoByCode($_GET['code']);
		
		if($code === false || $code['nombre'] == 0)
		{
			$_SESSION['user']['currentorder']['codepromo_code'] = '';
			$_SESSION['user']['currentorder']['codepromo_type'] = '';
			$_SESSION['user']['currentorder']['codepromo_param'] = '';
			$_SESSION['user']['currentorder']['frais_port_apresreduction'] = $_SESSION['user']['currentorder']['frais_port_avantreduction'];
			$_SESSION['user']['currentorder']['montantpanier_apresreduction'] = $_SESSION['user']['currentorder']['montantpanier_avantreduction'];
			echo 'Code invalide';
		}
		else
		{
			if(isset($_SESSION['user']['currentorder']['adresse_pays']) == false)
				$FraisPort = false;
			else
			{
				$FraisPort = GetFraisportByCountry($_SESSION['user']['currentorder']['adresse_pays']);
				if($FraisPort)
					$FraisPort = $FraisPort['fraisport'];
			}
			
			$_SESSION['user']['currentorder']['codepromo_code'] = $code['code'];
			$_SESSION['user']['currentorder']['codepromo_type'] = $code['type'];
			$_SESSION['user']['currentorder']['codepromo_param'] = $code['param'];
			
			switch($code['type'])
			{
				case 'Reduction fixe':
				{
					$Reduction = $code['param'] . ' €';
					if($FraisPort)
					{
						$_SESSION['user']['currentorder']['frais_port_apresreduction'] = $_SESSION['user']['currentorder']['frais_port_avantreduction'];
						$_SESSION['user']['currentorder']['montantpanier_apresreduction'] = $_SESSION['user']['currentorder']['montantpanier_avantreduction'] - $code['param'];
						$NouveauTotal = ($_SESSION['user']['currentorder']['montantpanier_avantreduction'] - $code['param'] + $FraisPort) . ' €';
					}
					else
						$NouveauTotal = 'Erreur';
						
					break;
				}
				
				case 'Pourcentage':
				{
					$Reduction = $code['param'] . ' %';
					if($FraisPort)
					{
						$_SESSION['user']['currentorder']['frais_port_apresreduction'] = $_SESSION['user']['currentorder']['frais_port_avantreduction'];
						$_SESSION['user']['currentorder']['montantpanier_apresreduction'] = $_SESSION['user']['currentorder']['montantpanier_avantreduction'] * (1-$code['param'] / 100);
						$NouveauTotal = ($_SESSION['user']['currentorder']['montantpanier_avantreduction'] * (1-$code['param'] / 100) + $FraisPort) . ' €';
					}
					else
						$NouveauTotal = 'Erreur';
						
					break;
				}
					
				default:
				{
					$Reduction = 'Port offert';
					$_SESSION['user']['currentorder']['frais_port_apresreduction'] = 0;
					$_SESSION['user']['currentorder']['montantpanier_apresreduction'] = $_SESSION['user']['currentorder']['montantpanier_avantreduction'];
					$NouveauTotal = $_SESSION['user']['currentorder']['montantpanier_avantreduction'] . ' €';
					break;
				}
			}
			
			echo '
				<div id="panier_etape4_texte2">
					<div id="panier_etape4_txtreduction">' . $Reduction . '</div>
					<div id="panier_etape4_txtnouveautotal">' . $NouveauTotal . '</div>
				</div>';
		}
	}
	
?>